﻿using System.Collections;
using System.Linq;
using Pap.Core.Utilities;
using Pap.Data;
using Pap.Services.FinancialServices;
using Pap.Web.Utils;
using System.Collections.Generic;

namespace Pap.Web.Modules.Loan.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class AssociationsBankAccountsViewModel : IViewModel
    {
        /// <summary>
        /// 
        /// </summary>
        IFinanceService _FinanceService;
        /// <summary>
        /// 
        /// </summary>
        IAdministrationRepository _AdministrationRepository;
        /// <summary>
        /// Initializes a new instance of the <see cref="AssociationsBankAccountsViewModel"/> class.
        /// </summary>
        /// <param name="financeService">The finance service.</param>
        /// <param name="administrationRepository">The administration repository.</param>
        public AssociationsBankAccountsViewModel(IFinanceService financeService, IAdministrationRepository administrationRepository)
        {
            _FinanceService = financeService;
            _AdministrationRepository = administrationRepository;
        }

        /// <summary>
        /// Gets the committees bank accounts.
        /// </summary>
        /// <returns></returns>
        public IEnumerable GetCommitteesBankAccounts()
        {
            IEnumerable<IProject> userProjects = _AdministrationRepository.GetUserProjects(RegistryInfo.UserID);
            var bankAccounts = _AdministrationRepository.FindCommitteeBankAccounts(bAccount => userProjects.Contains(bAccount.Project)).OrderBy(account => account.Committee.CommitteeName);
            //_AdministrationRepository.GetCommitteeBankAccountDepositReceiptByID()
            return from bankAccount in bankAccounts
                   select new
                   {
                       CommitteeID = bankAccount.CommitteeID,
                       CommitteeName = bankAccount.Committee.CommitteeName,
                       ProjectName = bankAccount.Project.ProjectName,
                       Balance = bankAccount.Balance,
                       Deposit = _FinanceService.GetPaidReciepts(bankAccount.ProjectID).Sum(r=>r.ReceiptAmount)
                   };
        }
    }
}
